package com.linkit.main.article;

import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linkit.config.MediaType;
import com.linkit.main.MainActivity;
import com.linkit.main.R;
import com.linkit.main.widget.ExtendedImageView;
import com.linkit.model.Article;
import com.linkit.utils.Utils;

/**
 * adapter hiện thị danh sách các hãng xe trong tuyến
 * */
public class ArticlesAdapter extends BaseAdapter{

	private MainActivity context;
	private List<Article> items;

	public ArticlesAdapter(List<Article> items) {
		this.context = MainActivity.getInstance();
		this.items = items;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ArticleViewHolder holder = null;
		final Article rowItem = getItem(position);

		LayoutInflater mInflater = context.getLayoutInflater();
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.article_item, null);
			holder = new ArticleViewHolder();
			holder.title = (TextView) convertView
					.findViewById(R.id.news_title);
			holder.time = (TextView) convertView
					.findViewById(R.id.news_time);
			
			holder.imageView = (ExtendedImageView) convertView
					.findViewById(R.id.news_image);
			
			holder.views = (TextView) convertView
					.findViewById(R.id.news_views);
			
			holder.comment = (TextView) convertView
					.findViewById(R.id.news_comment);
			
			holder.commentBtn = (LinearLayout) convertView
					.findViewById(R.id.news_comment_btn);
			
			convertView.setTag(holder);
		} else{
			holder = (ArticleViewHolder) convertView.getTag();
		}
			

		holder.title.setText(rowItem.title);
		holder.time.setText(Utils.formatTime(new Date(rowItem.time)));
		
		if(rowItem.avatar != null){
			new ImageLoader(rowItem.type, holder.imageView).execute(rowItem.avatar);
		}
		
		if(rowItem.type == MediaType.VIDEO.getId()){
			holder.imageView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Log.e("",rowItem.content);
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(rowItem.content));
					context.startActivity(intent);
				}
			});
		}
		
		
		if(!rowItem.isLoadLink){
//			new FacebookLinkLoader(rowItem, holder).execute();
		}else{
			holder.views.setText("" + rowItem.viewCount);
			holder.comment.setText("" + rowItem.commentCount);
		}
		
		holder.commentBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				ArticleComments articleComments = new ArticleComments();
				articleComments.showPage(rowItem);
			}
		});
		
		return convertView;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Article getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}